package com.xoa.controller.knowledge;

import com.xoa.dao.department.DepartmentMapper;
import com.xoa.model.department.Department;
import com.xoa.model.knowledge.KnowledgeCustomer;
import com.xoa.model.users.Users;
import com.xoa.service.knowledge.ClientService;
import com.xoa.util.LimsJson;
import com.xoa.util.ToJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 知识库-客户管理控制器
 */
@Controller
@RequestMapping("/client")
public class ClientContraller {
    @Autowired
    private ClientService clientService;

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 知识库-客户管理页面映射
     */
    @RequestMapping("/clientIndex")
    public String toClientIndex() { return "/app/knowledge/clientManager/client"; }
    /**
     * 知识库-新建客户
     */
    @RequestMapping("/addClient")
    public String addClient() { return "/app/knowledge/clientManager/addClient"; }

    /**
     *客户订阅一栏，查看所有文档页面映射
     */
    @RequestMapping("/showSubDocfile")
    public String showSubDocfile() { return "/app/knowledge/columnSubscription/showSubDocfile"; }



    /**
     * 知识库-客户管理 页面表格初始化数据
     * @param customer
     * @return
     */
    @RequestMapping("/getAllCustomer")
    @ResponseBody
    public ToJson getAllCustomer(KnowledgeCustomer customer,Integer page,Integer limit,String isSysCode){
        return clientService.getAllCustomer(customer,page,limit,isSysCode);
    }
    /**
     * 知识库-客户管理 根据id查询
     * @param
     * @return
     */
    @RequestMapping("/getCustomerById")
    @ResponseBody
    public ToJson getCustomerById(Integer customerId){
        return clientService.getCustomerById(customerId);
    }

    @RequestMapping("/addCustomer")
    @ResponseBody
    public ToJson addCustomer(HttpServletRequest request,KnowledgeCustomer customer,String byName,String password,String userName){
        Users users = new Users();
        //users.setUserPriv(userPriv);
        users.setPassword(password);
        List<Department> deptList = departmentMapper.selectObjectByParent(99999997);
        if(deptList==null||deptList.size()<1){ //没有客户部门就新建
            Department addDepart = new Department();
            addDepart.setDeptParent(99999997);
            addDepart.setDeptName("客户");
            addDepart.setDeptType("0");
            addDepart.setgDept((byte) (0 & 0xff));
            addDepart.setClassifyOrg(0);
            departmentMapper.insertDept(addDepart);
            deptList = departmentMapper.selectObjectByParent(99999997);
        }
        if(new Integer(99999997).equals(deptList.get(0).getDeptParent())){
            users.setDeptId(deptList.get(0).getDeptId()); //99999997代表客户部门
            users.setByname(byName);
            users.setUserName(userName);
            users.setPostId(0);
            users.setNotLogin((byte) (0 & 0xff));
            return clientService.addCustomer(request,customer,users);
        }else{
            return new ToJson(1,"客户部门不存在");
        }
    }

    @RequestMapping("/delCustomer")
    @ResponseBody
    public ToJson delCustomer(HttpServletRequest request,String ids){
        return clientService.delCustomer(request,ids);
    }

    @RequestMapping("/editCustomer")
    @ResponseBody
    public ToJson editCustomer(HttpServletRequest request,KnowledgeCustomer customer,String byName,String password,Integer userPriv,Integer deptId,String userName,String userId){
        Users users = new Users();
        users.setUserId(userId);
        users.setUserPriv(userPriv);
        users.setPassword(password);
        users.setDeptId(deptId);
        users.setByname(byName);
        users.setUserName(userName);
        users.setPostId(0);
        users.setNotLogin((byte) (0 & 0xff));
        return clientService.editCustomer(request,customer,users);
    }


    /**
     * 查看用户下载查看文档记录
     * @param request
     * @param userId
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/getLogByUserId")
    @ResponseBody
    public LimsJson getLogByUserId(HttpServletRequest request, String userId, Integer page, Integer limit){
        return clientService.getLogByUserId(request,userId,page,limit);
    }
}
